package cn.stronglink.assetmanage.business.inspection.view.assetDetail;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.util.List;

import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.dao.AssetDao;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.SearchAssetDao;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.entity.SearchAsset;
import cn.stronglink.assetmanage.index.base.BaseChildFragment;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.assetmanage.utils.ImageUtils;

/**
 * 巡检资产详情
 * Created by zhengjinglan on 2018/6/23.
 */

public class PatrolAssetDetailFragment extends BaseChildFragment {

    private TextView tv_check_asset_name;   //资产名称
    private TextView sp_check_location;     //存放地点
    private TextView tv_check_asset_no;     //资产编码
    private TextView sp_brand_name;         //品牌
    private TextView sp_check_asset_class;   //资产类别
    private TextView tv_check_model;        //设备型号
    private TextView tv_check_sn_no;        //SN号
    private TextView tv_check_rfid_no;      //RFID号
    private ImageView iv_check_picture;     //照片
    private SearchAsset asset;    //资产实体类
    private TextView txt_title_left,txt_title_right,txt_main_title;
    @Nullable
    @Override

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_patrol_asset_detail, container, false);
        try {
            initViewAndData(view);
        }catch (Exception e){
            e.printStackTrace();
        }

        return view;
    }

    private void initViewAndData(View view){

        Bundle bundle = getArguments();
        if (bundle == null){
            Toast.makeText(this.getContext(), "传递的资产单参数为空，无法正常巡检",Toast.LENGTH_SHORT).show();

            return;
        }
        String sn = bundle.getString(Constants.PATROL_DETAIL_NO);
        if (sn == null || sn.isEmpty()){
            Toast.makeText(this.getContext(),"传递的资产编号为空，无法正常巡检",Toast.LENGTH_LONG).show();
            return;
        }
        txt_title_left = this.getActivity().findViewById(R.id.txt_left_title);
        txt_title_left.setVisibility(View.VISIBLE);
        txt_title_right = this.getActivity().findViewById(R.id.txt_right_title);
        txt_title_right.setVisibility(View.GONE);
        txt_main_title = this.getActivity().findViewById(R.id.txt_main_title);
        txt_main_title.setText("巡检资产详情");
        List<SearchAsset> assetList = DaoManager
                .getDaoSession(this.getContext()).getSearchAssetDao().queryBuilder().where(
                        SearchAssetDao.Properties.SerialNo.eq(sn)
                ).list();
        if (assetList!=null && assetList.size() >0){
            this.asset = assetList.get(0);
        }
        if (this.asset == null) {
            return;
        }

        this.tv_check_asset_name = view.findViewById(R.id.tv_check_asset_name);
        this.sp_check_location = view.findViewById(R.id.sp_check_location);
        this.tv_check_asset_no = view.findViewById(R.id.tv_check_asset_no);
        this.sp_brand_name = view.findViewById(R.id.sp_brand_name);
        this.sp_check_asset_class = view.findViewById(R.id.sp_check_asset_class);
        this.tv_check_model = view.findViewById(R.id.tv_check_model);
        this.tv_check_sn_no = view.findViewById(R.id.tv_check_sn_no);
        this.tv_check_rfid_no = view.findViewById(R.id.tv_check_rfid_no);
        this.iv_check_picture = view.findViewById(R.id.iv_check_picture);

        this.tv_check_asset_name.setText(asset.getAssetName());
        this.sp_check_location.setText(asset.getRoomName());
        this.tv_check_asset_no.setText(asset.getAssetNo());
        this.sp_brand_name.setText(asset.getBrandName());
        this.sp_check_asset_class.setText(asset.getBrandName());
        this.tv_check_model.setText(asset.getModelName());
        this.tv_check_sn_no.setText(asset.getSerialNo());
        this.tv_check_rfid_no.setText(asset.getRfidNo());
        String uri = asset.getPictureUri();
        if (uri!=null &&! "".equals(uri)){
            Bitmap bitmap = getPicture(uri);
            if (bitmap!= null){
                this.iv_check_picture.setVisibility(View.VISIBLE);
                this.iv_check_picture.setImageBitmap(bitmap);
            }
        }else {
            this.iv_check_picture.setVisibility(View.GONE);
        }



    }

    /**
     * 将URL转换成Bitmap
     */
    public Bitmap getPicture(String url){

        Bitmap bitmap = null;
        if(url!=null){
            try {
                bitmap =getBitmapByUrl(url);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        return bitmap;
    }

    /**
     * 获得图片BitMap
     * @param url
     * @return
     * @throws FileNotFoundException
     */
    public Bitmap getBitmapByUrl(String url) throws FileNotFoundException {
        return ImageUtils.getIntance().getimage(url);
    }
/*    @Override
    public void onResume() {
        super.onResume();
        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keycode, KeyEvent keyEvent) {
                if (keycode == KeyEvent.KEYCODE_BACK) {
                    mActivity.onBackPressed();
                    return true;
                }
                return false;
            }
        });
    }*/
    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }
    
}
